<?php

namespace structure\components;

class LinkTree extends Link
{
	/************************************************************
	*   					Properties							*
	************************************************************/


	/**
	 * Maximum depth allowed
	 *
	 * @var	integer
	 */
	private $depth = 1;

	/**
	 * Current link depth level
	 *
	 * @var	integer
	 */
	private $level = 0;

	/**
	 * Comma separated list of tags for each level
	 *
	 * @var	string
	 */
	private $levelDisplay = "";




	/************************************************************
	*   					Public methods						*
	************************************************************/


	/**
	 * The method is started automatically when starting component
	 *
	 * @return	string
	 */
	public function Start()
	{
		$comp = $this->GetComponent();

		$this->SetLevelDisplay($comp->GetAttribute("levelDisplay"));
		$this->SetPage($comp->GetAttribute("page"));
		$this->SetDepth($comp->GetAttribute("depth"));

		$res = $this->BuildSubPages();

		return $res;
	}




	/************************************************************
	*   					Protected methods					*
	************************************************************/


	/**
	 * Sets maximum tree depth level
	 *
	 * @param	integer	$level
	 * @return	integer
	 */
	protected function SetDepth($level)
	{
		$this->depth = $level;
		return true;
	}

	/**
	 * Returns maximum depth level
	 *
	 * @return	integer
	 */
	protected function GetDepth()
	{
		return $this->depth;
	}

	/**
	 * Sets level-specific tag wraps
	 *
	 * @param	string	$param
	 * @return	boolean
	 */
	protected function SetLevelDisplay($param)
	{
		$this->levelDisplay = explode(",", $param);
		return true;
	}

	/**
	 * Returns level-specific tag wrap
	 *
	 * @return	string
	 */
	protected function GetLevelDisplay()
	{
		$id = $this->level;

		if (isset($this->levelDisplay[$id]))
		{
			return (string)$this->levelDisplay[$id];
		}
		else
		{
			return false;
		}
	}

	/**
	 * Builds current page's subpages tree
	 *
	 * @return	string
	 */
	protected function BuildSubPages()
	{
		$sub = core("Page")->GetSubPages($this->GetPage());

		$res = "";

		foreach ($sub as $item)
		{
			$title = core("Page")->GetTitle($item['file_name']);

			$this->SetPage($item['file_name']);
			$this->SetTitle($title);

			if ($this->level + 1 < $this->GetDepth())
			{
				$this->level++;
				$subs = $this->BuildSubPages();
				$this->level--;
			}
			else
			{
				$subs = "";
			}

			//Handle possible overrides on the deeper levels
			$this->SetPage($item['file_name']);
			$this->SetTitle($title);

			//Building link
			$link = $this->BuildLink();

			if (is_object($link))
			{
				$link = $link->Build();
			}

			//print gettype($link)."<br />";

			//Applying level-specific wrap tags
			if ($tag = $this->GetLevelDisplay())
			{
				$link = "<$tag>$link</$tag>";
				//$wrap = core("Html")->CreateElement($tag);
				//$wrap->AddChild($link);
				//$link = $wrap->Build();
			}

			$res .= $link . $subs;
		}

		return $res;
	}


	/**
	 * Builds current page's subpages tree
	 *
	 * @return	string
	 */
	protected function BuildSubPagesArr()
	{
		$sub = core("Page")->GetSubPages($this->GetPage());

		$res = array();

		foreach ($sub as $item)
		{
			$title = core("Page")->GetTitle($item['file_name']);

			$this->SetPage($item['file_name']);
			$this->SetTitle($title);

			if ($this->level + 1 < $this->GetDepth())
			{
				$this->level++;
				$subs = $this->BuildSubPages();
				$this->level--;
			}
			else
			{
				$subs = array();
			}

			//Handle possible overrides on the deeper levels
			$this->SetPage($item['file_name']);
			$this->SetTitle($title);

			//Building link
			$link = $this->BuildLink();

			//Applying level-specific wrap tags
			if ($tag = $this->GetLevelDisplay())
			{
				$wrap = core("Html")->CreateElement($tag);
				$wrap->AddChild($link);
				$link = $wrap;
			}

			array_unshift($subs, $link);

			$res[] = $subs;
		}

		return $res;
	}
}